package test;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * 创建时间：2017年12月26日20:51 <br>
 * 作者：fuchaoyang <br>
 * 描述：将创建view的方法暴露出来，bindview直接写到getview里面，外界直接调用getview方法，不要直接inflate
 */

public abstract class RecyclerViewAdapter
  extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {

  public static final int TITLE = 1;
  public static final int CONTENT = 2;

  private Context mContext;
  private LayoutInflater mInflater;

  public RecyclerViewAdapter(Context context) {
    mContext = context;
    mInflater = LayoutInflater.from(context);
  }

  @Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    // 这里有header和content
    View itemView = mInflater.inflate(getLayoutId(viewType), parent, false);
    return new MyViewHolder(itemView, viewType);
  }

  @Override public void onBindViewHolder(MyViewHolder holder, int position) {
    holder.bindViewHolder(position);
  }

  @Override public int getItemViewType(int position) {
    return super.getItemViewType(position);
  }

  @Override public int getItemCount() {
    return 0;
  }

  protected abstract int getLayoutId(int viewType);

  public class MyViewHolder extends RecyclerView.ViewHolder {
    private View itemView;
    private int viewType;

    public MyViewHolder(View itemView, int viewType) {
      super(itemView);
      this.itemView = itemView;
      this.viewType = viewType;
    }

    public void bindViewHolder(int position/*, int viewType*/) {
      RecyclerViewAdapter.this.bindViewHolder(itemView, position, viewType);
    }
  }

  public abstract void bindViewHolder(View itemView, int position, int viewType);

  public void fun(int position) {
    View view = mInflater.inflate(getLayoutId(CONTENT), null, false);// view
    bindViewHolder(view, position, CONTENT);
  }
}
